package com.training.hatton.dropboxclient.utill;

import java.io.File;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.training.hatton.dropboxclient.ContextHolder;
import com.training.hatton.dropboxclient.content.contract.MetadataContract;
import com.training.hatton.dropboxclient.service.SyncService;

public class DropboxService {

	public static final String THUMBNAILS_BASE_URL = "https://api-content.dropbox.com/1/thumbnails/";
	
	public static final String TAG = "DROPBOX";
	public static final String APP_KEY = "ru7rb2rpn6p7a6o";
	public static final String APP_SECRET = "acbzt5g0s7sgxw9";
	public static final String DROPBOX_METADATA = "metadata";
	public static final String DROPBOX_PREFS_NAME = "dropbox";
	public static final String ACCESS_KEY_NAME = "token";
	public static final String ACCESS_SECRET_NAME = "secret";
	public static final String ROOT = "dropbox";

	public static final String EXTRA_ACTION = "action";
	public static final String EXTRA_ERROR = "error";	
	public static final String ACTION_DELETE = "delete";
	
	public static final String ROOT_METADATA = "{" +
			"\"size\": \"0 bytes\"," +
			"\"bytes\": 0," +
			"\"thumb_exists\": false," +
			" \"path\": \"/\"," +
			"\"is_dir\": true," +
			"\"icon\": \"folder_public\"," +
			"\"root\": \"dropbox\"," +
			"\"hash\": \"\"" +
			"}";

	public static final String EXTRA_JSON = "json";

	private static String access_token;
	private static String access_token_secret;
	
	private static DropboxAPI<AndroidAuthSession> mDBApi;

	public static DropboxAPI<AndroidAuthSession> getDBApi(){
		return mDBApi;
	}
	
	public static void setDBApi(DropboxAPI<AndroidAuthSession> api){
		mDBApi = api;
	}
	
	public static void logOut() {
		DropboxService.getDBApi().getSession().unlink();
		Intent intent = new Intent(ContextHolder.getInstance().getContext(), SyncService.class);
		ContextHolder.getInstance().getContext().stopService(intent);
		clearKeys();		
	}
	private static void clearKeys() {
		setKeys(null, null);
		SharedPreferences prefs = ContextHolder.getInstance().getContext().getSharedPreferences(
				DropboxService.DROPBOX_PREFS_NAME, 0);
		Editor edit = prefs.edit();
		edit.clear();
		edit.commit();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				ContextHolder.getInstance().getContext().getContentResolver()
					.delete(MetadataContract.MetadataColumns.CONTENT_URI, null, null);				
				File[] files = ContextHolder.getInstance().getContext().getCacheDir().listFiles();
				for (File file : files) {
					file.delete();
				}
			}
		});
	}

	public static void setKeys(String key, String secret) {
		access_token = key;
		access_token_secret = secret;
	}

	public static String getAccess_token() {
		return access_token;
	}

	public static String getAccess_token_secret() {
		return access_token_secret;
	}

	public static boolean isAuthorizated() {
		if (getAccess_token()!=null && getAccess_token_secret() != null){
			return true;
		}
		return false;
	}
}